var elPhone            = document.getElementById('phone'),
    elPhoneMsg         = document.getElementById('phoneMsg'),
    elCheckCode        = document.getElementById('checkCode'),
    elCheckCodeMsg     = document.getElementById('checkCodeMsg'),
    elSendButton       = document.getElementById('send');

var phoneFlag  = false,
    checkCodeFlag  = false;

var xhr  = '';

function checkPhone(phone) {
    if(phone === null || phone === '' || phone.length === 0){
        elPhoneMsg.innerHTML = "<p style='color: orange'>请输入手机号码</p>";
        phoneFlag = false;
    } else if (phone.length !== 11){
        elPhoneMsg.innerHTML = "<p style='color=red'>手机号码不合法</p>";
        phoneFlag = false;
    }else{
        elPhoneMsg.innerHTML = "<p style='color=green'>手机号码合法</p>";
        phoneFlag = true;
    }
}

function Time() {
    var signOnMsg = $("#signOnMsg").text();
    if (signOnMsg === "该手机号无管理员账号") {
        checkTime(2);
    } else if (signOnMsg === "验证码已发送") {
        checkTime(60);
    } else if (signOnMsg === "验证码发送失败，请稍后再试") {
        checkTime(2);
    }
}

function checkTime(time) {
    if (time === 0) {
        elSendButton.removeAttr("disabled");//这个表示，从xx中移除yy属性，$(xx).remove()，移除xx元素
        elSendButton.val("重新发送");
    } else {
        time--;
        elSendButton.attr("disabled", true);
        elSendButton.val("" + time + "s");
        setTimeout(function () {
            checkTime(time);
        }, 1000);
    }
}

function checkCode(code){
    if (elPhone.value.trim().length === 0
        || elPhone.value.trim().length !== 11) {
        elPhoneMsg.innerHTML = "<p style='color=red'>手机号码不正确</p>";
        phoneFlag = false;
    }
    if (code.length !== 4) {
        elCheckCodeMsg.innerHTML = "<p style='color=red'>验证码输入有误</p>";
        checkCodeFlag = false;
    }else{
        phoneFlag = true;
        checkCodeFlag = true;
    }
}

elPhone.addEventListener('blur', function(){
    var phone = elPhone.value.trim();
    checkPhone(phone);
});

elCheckCode.addEventListener('blur', function(){
    var code = elCheckCode.value.trim();
    checkCode(code);
});